package zhoug.zui.dialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.TransformationMethod;
import android.widget.EditText;

import zhoug.logging.Logger;
import zhoug.zui.R;
import zhoug.zui.utils.ZUIUtils;

import androidx.annotation.NonNull;

/**
 * 带输入的对话框
 * 全局样式：
 * zui_style_ZUIInputDialog
 *
 * @Author 35574
 * @Date 2020/7/23
 * @Description
 */
public class ZUIInputDialog extends ZUIButtonDialog {
    private static final String TAG = ">>>ZUIInputDialog";
    private static final boolean DEBUG = false;
    private EditText mEditText;
    /***最大行数*/
    private int maxLines;
    /***文字大小*/
    private float textSize;
    /***文字颜色*/
    private int textColor;
    /***提示文字颜色*/
    private int hintColor;
    /***输入类型*/
    private int inputType;
    /***输入文本*/
    private String inputText;
    /***提示文本*/
    private String hintText;

    private TransformationMethod transformationMethod;

    public ZUIInputDialog(@NonNull Context context) {
        this(context, 0);
    }

    public ZUIInputDialog(@NonNull Context context, int themeResId) {
        this(context, themeResId, R.attr.ZUIInputDialog_theme, R.style.ZUIInputDialog_def);
    }

    protected ZUIInputDialog(@NonNull Context context, int themeResId, int defStyleAttr, int defStyleRes) {
        super(context, themeResId, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        super.initialize(context, defStyleAttr, defStyleRes);
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZUIInputDialog, defStyleAttr, defStyleRes);
        textSize = a.getDimension(R.styleable.ZUIInputDialog_zui_textSize, 0);
        textColor = a.getColor(R.styleable.ZUIInputDialog_zui_textColor, ZUIUtils.UN_USED_COLOR);
        hintColor = a.getColor(R.styleable.ZUIInputDialog_zui_hintColor, ZUIUtils.UN_USED_COLOR);
        maxLines = a.getInt(R.styleable.ZUIInputDialog_android_maxLines, 0);
        a.recycle();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.zui_input_dialog;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        if (DEBUG) {
            Logger.d(TAG, "init:maxLines=" + maxLines);
            Logger.d(TAG, "init:inputType=" + inputType);
        }
        mEditText = findViewById(R.id.editText);
        if (maxLines >= 1) {
            mEditText.setMaxLines(maxLines);
        } else {
            mEditText.setMaxLines(1);
            mEditText.setSingleLine(true);
        }
        ZUIUtils.setTextColor(mEditText, textColor);
        ZUIUtils.setHintTextColor(mEditText, hintColor);
        ZUIUtils.setTextSize(mEditText, textSize);
        if (inputType != InputType.TYPE_NULL) {
            mEditText.setInputType(inputType);
        }
        if(transformationMethod!=null){
            mEditText.setTransformationMethod(transformationMethod);
        }
        mEditText.setText(inputText);
        mEditText.setHint(hintText);

        super.init(savedInstanceState);
    }

    /**
     * 获取输入的值
     *
     * @return 默认""
     */
    public String getInputText() {
        if (mEditText != null) {
            return mEditText.getText().toString();
        }
        return "";
    }

    /**
     * 设置输入类型
     * 注:setInputType后maxLines失效的问题解决:
     * setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
     * @param inputType {@link InputType}
     */
    public void setInputType(int inputType) {
        this.inputType = inputType;
    }

    /**
     * 提示文本
     * @param hintText
     */
    public void setHintText(String hintText) {
        this.hintText = hintText;
    }

    /**
     * 提示文本颜色
     * @param hintColor
     */
    public void setHintColor(int hintColor) {
        this.hintColor = hintColor;
    }

    /**
     * 输入文字颜色
     * @param textColor
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    /**
     * 输入文字大小
     * @param textSize
     */
    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    /**
     * 输入文字
     * @param inputText
     */
    public void setInputText(String inputText) {
        this.inputText = inputText;
    }

    /**
     *
     * @param transformationMethod {@link TransformationMethod} eg:{@link android.text.method.PasswordTransformationMethod}
     */
    public void setTransformationMethod(TransformationMethod transformationMethod) {
        this.transformationMethod = transformationMethod;
    }

}
